﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SharePointLogBrowser2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            foldersAndFiles.SelectedFileChanged += SelectedFileChanged;
        }

        private void SelectedFileChanged(object sender, EventArgs e)
        {
            ReloadFile();
        }


        private void btnAddTextFilter_Click(object sender, EventArgs e)
        {
            LogItem item = lvItems.GetSelectedLogItem();
            AddTextFilter filter = new AddTextFilter();
            filter.Owner = this;
            if (item != null)
            {
                filter.FilterText = item.Message;
            }
            if (filter.ShowDialog() == DialogResult.OK)
            {
                LogItemContext.Current.AddTextFilter(filter.FilterText);
            }

        }

        private void btnFilterItems_Click(object sender, EventArgs e)
        {
            lvItems.SetItemsToListView(LogItemContext.Current.Filter(
                cbFilterByTextFilter.Checked,
                cbFilterItems.Checked,
                filters.GetFilterItemByArea(),
                filters.GetFilterItemByCategory(),
                filters.GetFilterItemByLevel(),
                filters.GetFilterItemByProcess()));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbxCorrelation.Text.Trim()))
            {
                LogItem item = lvItems.GetSelectedLogItem();
                if (item != null)
                {
                    ShowViewByCorrelation(item.Correlation);
                }
            }
            else
            {
                ShowViewByCorrelation(tbxCorrelation.Text.Trim());
            }
        }

        private void ShowViewByCorrelation(string correlation)
        {
            ViewByCorrelation form = new ViewByCorrelation();
            form.Owner = this;

            form.Correlation = correlation;

            form.ShowDialog();
        }

        private void btnFindCorrelation_Click(object sender, EventArgs e)
        {
            lvItems.FindNextCorrelation(tbxCorrelation.Text.Trim());
        }

        private void btnReloadFile_Click(object sender, EventArgs e)
        {
            ReloadFile();
        }

        private void ReloadFile()
        {
            List<FileInfo> fileInfos = foldersAndFiles.SelectedFiles;

            tabControl.SelectedIndex = 1;

            if (fileInfos.Count > 0)
            {
                this.Text = "SharePoint Log Browser " + Path.GetFileNameWithoutExtension(fileInfos[0].Name);
                LogItemContext.Current.ReadAll(fileInfos);
            }
            else
            {
                this.Text = "SharePoint Log Browser";
                LogItemContext.Current.ClearLogItems();
            }
            lvItems.SetItemsToListView(LogItemContext.Current.Items);
            filters.SetFilters();
        }
    }
}
